using System.Diagnostics;

namespace 灰度算法
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string filePath = "";
        Bitmap qc = null;
        int xj = 0;
        private void textBox1_Click(object sender, EventArgs e)
        {
            //创建文件弹出选择窗口（包括文件名）对象
            OpenFileDialog ofd = new OpenFileDialog();
            //判断选择的路径
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName.ToString();
                pictureBox1.Image = Image.FromFile(ofd.FileName.ToString());

            }
            filePath = textBox1.Text;
        }
              

        private void button2_Click(object sender, EventArgs e)
        {

            Stopwatch sw = new Stopwatch();
            sw.Start();
            xj = 0;
            Bitmap b = new Bitmap(filePath);
            var hs= ToGray(b);
            pictureBox2.Image = hs;
            int iss = int.Parse(textBox2.Text);
            var erz=ConvertTo1Bpp(hs, iss);
            pictureBox3.Image = erz;
            decimal zxs = erz.Width * erz.Height;
            decimal bfb = xj / zxs * 100;
            label3.Text=bfb.ToString();
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            label2.Text = $"执行总共花费{ts2.TotalMilliseconds}ms.";
        }
        /// <summary>
        /// 图像灰度化算法（加权）
        /// </summary>
        /// <param name="inputBitmap">输入图像</param>
        /// <param name="outputBitmap">输出图像</param>
        public static Bitmap ToGray(Bitmap inputBitmap)
        {
            for (int i = 0; i < inputBitmap.Width; i++)
            {
                for (int j = 0; j < inputBitmap.Height; j++)
                {
                    Color color = inputBitmap.GetPixel(i, j);  //在此获取指定的像素的颜色
                    int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
                    Color newColor = Color.FromArgb(gray, gray, gray);
                    //设置指定像素的颜色  参数：坐标x,坐标y，颜色
                    inputBitmap.SetPixel(i, j, newColor);
                }
            }
            return inputBitmap;
        }
        /// <summary>
        /// 图像二值化：取图片的阈值，低于该值的全都为0，高于该值的全都为255 输出锈迹为绿色
        /// </summary>
        /// <param name="bmp">输入图像</param>
        /// <param name="iss">阈值</param>
        /// <param name="outputBmp">输出图像</param>
        /// <returns></returns>
        public  Bitmap ConvertTo1Bpp(Bitmap bmp, int iss)
        {
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    //获取该点的像素的RGB的颜色
                    Color color = bmp.GetPixel(i, j);
                    int value = 255 - color.B;
                    //Color newColor = value > iss ? Color.FromArgb(0, 255, 0) : Color.FromArgb(255,255, 255);
                    Color newColor = Color.FromArgb(0, 255, 0);
                    if (value>iss)
                    {
                        xj++;
                        newColor=Color.FromArgb(0, 255, 0);
                    }
                    else
                    {
                        newColor=Color.FromArgb(255, 255, 255);
                    }
                    bmp.SetPixel(i, j, newColor);
                }
            }
            return  bmp;
        }

    }
}